package com.third.entity;

import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONWriter;

/**
 * @author 唐博文
 * @version 2023.1.2
 * {@code @date} 2023/10/22 15:32 星期日
 */
public record RestBean<T>(int code,T data,String message) {
    public static  <T> RestBean<T> success(T data){
        return new RestBean<>(200,data,"请求成功");
    }
    
    public static <T> RestBean<T> success(){
        return success(null);
    }
    
    public static <T> RestBean<T> unauthorized(String message){
        return failure(401,message);
    }
    
    public static <T> RestBean<T> forbidden(String message){
        return failure(403,message);
    }
    
    public static  <T> RestBean<T> failure(int code,String message){
        return new RestBean<>(code,null,message);
    }
    
    
    public String asJsonString(){
        return JSONObject.toJSONString(this, JSONWriter.Feature.WriteNulls);
    }
    
}
